﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace AirplaneReservation
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            for (int i = 18; i < 90; i++)
			{
                age.Items.Add(i.ToString());
			}

            if (Session["ssRole"].ToString() == "member")
            {
                SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
                ccon.Open();

                string select = "select UserName, Password, FirstName, LastName, Address, PhoneNumber, Email, Sex, Age, CreditCard, SkyMiles, Passport from Member join Customer on Member.Id=Customer.MemberId where UserName='" + Session["ssUser"] + "' ";
                SqlDataAdapter da = new SqlDataAdapter(select, ccon);
                DataTable dt = new DataTable();
                da.Fill(dt);

                userName.Text = dt.Rows[0][0].ToString();
                passW.Text = dt.Rows[0][1].ToString();
                firstN.Text = dt.Rows[0][2].ToString();
                lastN.Text = dt.Rows[0][3].ToString();
                address.Text = dt.Rows[0][4].ToString();
                phone.Text = dt.Rows[0][5].ToString();
                email.Text = dt.Rows[0][6].ToString();
                if (dt.Rows[0][7].ToString()=="True")
                   sex.SelectedItem.Text = "Male";
                else sex.SelectedItem.Text = "Female";
                age.SelectedItem.Text = dt.Rows[0][8].ToString();
                creditCard.Text = dt.Rows[0]["CreditCard"].ToString();
                mile.Text = dt.Rows[0]["SkyMiles"].ToString();
                passport.Text = dt.Rows[0]["Passport"].ToString();
            }
        }

        protected void btnLogIn_Click(object sender, EventArgs e)
        {
            SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
            ccon.Open();

            string select = "select UserName, Password, Name from Member join Role on Member.RoleId=Role.Id where UserName='" + txtUser.Text + "' and Password='" + txtPassword.Text + "' ";
            SqlDataAdapter da = new SqlDataAdapter(select, ccon);
            DataTable dt = new DataTable();
            da.Fill(dt);

            int count = dt.Rows.Count;
            if (count == 0)
            {
                lbError.Text = "Wrong user name or password...";
            }
            else
            {
                if (dt.Rows[0]["Name"].ToString() == "Member")
                {
                    Session["ssUser"] = dt.Rows[0]["UserName"].ToString();
                    Session["ssRole"] = "member";
                    Response.Redirect("~/Default.aspx");
                }
            }
            
            ccon.Close();
        }

        protected void logOut_Click(object sender, ImageClickEventArgs e)
        {
            Session.Abandon();
            Response.Redirect("Default.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            if (btnEdit.Text == "Edit")
            {
                btnEdit.Text = "Update";
                passW.ReadOnly = false;
                address.ReadOnly = false;
                phone.ReadOnly = false;
                email.ReadOnly = false;

                SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
                ccon.Open();

                string select = "insert into Member(Password, Credit Card) values ('"+passW.Text+"', '"+creditCard.Text+"') where ";
                SqlCommand cm = new SqlCommand(select, ccon);
                cm.ExecuteNonQuery();

                string sql = "insert into Custommer(Adress, PhoneNumber, Email, Age)(Password, Credit Card) values ('" + passW.Text + "', '" + creditCard.Text + "')";
                SqlCommand cm2 = new SqlCommand(select, ccon);
                cm2.ExecuteNonQuery();
            }
            else
            {
                btnEdit.Text = "Edit";
                passW.ReadOnly = true;
                address.ReadOnly = true;
                phone.ReadOnly = true;
                email.ReadOnly = true;
            }
        }

        void enableTextBox()
        {
            passW.ReadOnly = false;
            address.ReadOnly = false;
            phone.ReadOnly = false;
            email.ReadOnly = false;
        }

        void disableTextBox()
        {
            passW.ReadOnly = true;
            address.ReadOnly = true;
            phone.ReadOnly = true;
            email.ReadOnly = true;
        }
    }
}